/**
 * @author Stephan Pfaff (http://www.derdaumen.net)
 */

/**
 * The Project Object
 * @param vars object providing the project properties (loaded from the database)
 * @return
 */
function Project(vars, params, parent){
	if (vars != null) {
		try {
			Item.call(this, vars.prefix, vars.id, vars.type, vars.basename, vars.path, vars.label, vars.alias, vars.thumb, vars.thumb_enable, false, vars.item_class);
			this.category_id 	= parseInt(vars.category_id);
			this.position		= vars.position*site.gridSize;
			this.link			= vars.link;
			this.link_enable	= vars.link_enable;
			this.bg_active 		= '#232323';
			this.bg_inactive 	= '#232323';
			this.border_active 	= '#0f0';
			this.border_inactive = '#999';
			
			//Überprüfung ob das element per php oder javascript erstellt wird
			if(noPHPMode) {
				//seite läuft koplett über AJAX, element muss per javascript erstellt werden
				var div_id = this.prefix+"_"+this.id;
				var div_class = params.prj_id == this.id ? 'item '+this.prefix+' active' : 'item '+this.prefix;
				var html 		= ["<div id='"+div_id+"'"+"class='"+div_class+"'"+"style='top: "+this.position+"px;'>\n"];
				if(this.thumb != "" && this.thumb_enable) 	html.push("	<img id='thumb_"+div_id+"' class='thumb' src='"+this.thumb+"' alt='"+this.alias+"'/>\n");
				if(this.label != "") 						html.push("	<div id='label_"+div_id+"' class='label'>"+this.label+"</div>\n");
				if(this.link != null && this.link_enable) 	html.push("	<a id='outerlink_"+this.div_id+"' class='link nav outer' href='"+this.link+"' target='_blank'></a>\n");
				html.push("</div>\n");
				this.$div = $(html.join(''));
				parent.$scrollWrapper.append(this.$div);
			} else {
				this.$div = $("#"+this.prefix+"_"+this.id);
			}
			
			//bei aktivem noPHPMode das Element draggable machen
			if(noPHPMode && allowDraggable)
				this.$div.draggable({
					revert: false,
					cancel: '.active',
					zIndex: 100
				});
			
			//content laden, falls das element aktiv ist
			if(this.id == params.prj_id)
				this.loadChilds(params);
			
			//zeiger auf das objekt um auch innerhalb von inline methoden darauf zugreifen zu können
			var handle = this;
			
			//hover
			this.$div.hover(
				function(event) {
					if(!handle.locked && !$(this).hasClass("active"))
					$(this).stop().animate({
						//outlineColor: handle.border_active
						borderTopColor: handle.border_active,
						borderRightColor: handle.border_active,
						borderBottomColor: handle.border_active,
						borderLeftColor: handle.border_active
					}, site.navAnimationDuration);
				},
				function(event) {
					if(!handle.locked && !$(this).hasClass("active"))
					$(this).stop().animate({
						//outlineColor: handle.border_inactive
						borderTopColor: handle.border_inactive,
						borderRightColor: handle.border_inactive,
						borderBottomColor: handle.border_inactive,
						borderLeftColor: handle.border_inactive
					}, site.navAnimationDuration);
			});
			//click
			this.$div.click(function() {
				if(noPHPMode) {
					if(!$(this).hasClass("active")) {
						handle.loadChilds(params);
						$(site.projects.$div).find(".active").animate({
							borderTopColor: '#999',
							borderRightColor: '#999',
							borderBottomColor: '#999',
							borderLeftColor: '#999'
						}, site.navAnimationDuration).removeClass('active');
						$(this).addClass("active");
					} else {
						handle.loadChilds(params);
					}
				}
			});
			
		} catch(e) {
			if(debug) alert('Initialization of Project failed, missing or wrong parameter! ('+e+')');
		}
	}
};

Project.prototype = new Item();
Project.prototype.category_id = 0;
Project.prototype.position = 0;
Project.prototype.link = "";
Project.prototype.link_enable = false;
Project.prototype.html = "";
Project.prototype.$div = null;
Project.prototype.bg_active = null;
Project.prototype.bg_inactive = null;
Project.prototype.border_active = '';
Project.prototype.border_inactive = '';

/**
 * Loads the project's children.
 */
Project.prototype.loadChilds = function(params) {
	if(noPHPMode) {
		params = {ctg_id: this.category_id,	prj_id: this.id};
	}
	site.buttons.load(params, false);
};